﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Common;
using System.Xml.Linq;
using System.IO;

namespace Model
{
   public class PropertyGridDAO
    {
        /// <summary>
        /// 通过控件模板节点种类获取信息
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="PropertyType"></param>
        /// <returns></returns>
        public List<eProperty> GetControlTemplateInfoByPropertyType(XmlDocument doc, enumPropertyType PropertyType)
        {
            try
            {
                List<eProperty> list = new List<eProperty>();
                eProperty ePro;
                XmlNodeList xnl = doc.SelectNodes("//Object//" + PropertyType.ToString());
                foreach (XmlNode xn in xnl)
                {
                    ePro = new eProperty();
                    ePro.Name = xn.Attributes["name"] == null ? "" : xn.Attributes["name"].Value;
                    ePro.PropertyType = PropertyType;
                    ePro.Value = xn.Attributes["value"] == null ? "" : xn.Attributes["value"].Value;
                    ePro.ValueType = xn.Attributes["valueType"] == null ? "" : xn.Attributes["valueType"].Value;
                    ePro.ValueTypeFullName = xn.Attributes["fullType"] == null ? "" : xn.Attributes["fullType"].Value;
                    ePro.Accessibility = xn.Attributes["accessibility"] == null ? "" : xn.Attributes["accessibility"].Value;
                    ePro.ReturnType = xn.Attributes["returnType"] == null ? "" : xn.Attributes["returnType"].Value;
                    ePro.Attribute = xn.Attributes["attribute"] == null ? "" : xn.Attributes["attribute"].Value;
                    ePro.EventArgs = xn.Attributes["eventArgs"] == null ? "" : xn.Attributes["eventArgs"].Value;
                    list.Add(ePro);
                }
                return list;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取控件模板所有信息
        /// </summary>
        /// <param name="doc"></param>
        /// <returns></returns>
        public List<eProperty> GetControlTemplateInfo(XmlDocument doc)
        {
            try
            {
                List<eProperty> list = new List<eProperty>();
                eProperty ePro;
                XmlNodeList xnl = doc.SelectSingleNode("//Object").ChildNodes;
                foreach (XmlNode xn in xnl)
                {
                    ePro = new eProperty();
                    ePro.Name = xn.Attributes["name"] == null ? "" : xn.Attributes["name"].Value;
                    ePro.PropertyType = (enumPropertyType)Enum.Parse(typeof(enumPropertyType), xn.Name, false);
                    ePro.Value = xn.Attributes["value"] == null ? "" : xn.Attributes["value"].Value;
                    ePro.ValueType = xn.Attributes["valueType"] == null ? "" : xn.Attributes["valueType"].Value;
                    ePro.ValueTypeFullName = xn.Attributes["fullType"] == null ? "" : xn.Attributes["fullType"].Value;
                    ePro.Accessibility = xn.Attributes["accessibility"] == null ? "" : xn.Attributes["accessibility"].Value;
                    ePro.ReturnType = xn.Attributes["returnType"] == null ? "" : xn.Attributes["returnType"].Value;
                    ePro.Attribute = xn.Attributes["attribute"] == null ? "" : xn.Attributes["attribute"].Value;
                    ePro.EventArgs = xn.Attributes["eventArgs"] == null ? "" : xn.Attributes["eventArgs"].Value;
                    list.Add(ePro);
                }
                return list;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


       /// <summary>
       /// 获取文档模板所有信息
       /// </summary>
       /// <param name="doc"></param>
       /// <returns></returns>
        public List<eProperty> GetDocumentTemplateInfo(XmlDocument doc,enumBaseControlType enumBase)
        {
            try
            {
                List<eProperty> list = new List<eProperty>();
                eProperty ePro;

                XmlNodeList xnlBase = doc.SelectSingleNode("//Document").ChildNodes;
                foreach (XmlNode xnBase in xnlBase)
                {
                    if (String.Compare(xnBase.Attributes["type"].Value, enumBase.ToString(), false) == 0)
                    {
                        foreach (XmlNode xn in xnBase.ChildNodes)
                        {   
                            ePro = new eProperty();
                            //方法的参数列表
                            if (xn.HasChildNodes && String.Compare(xn.Name, "Method", false) == 0)
                            {   
                                XmlNodeList xnChilds = xn.SelectNodes("Parameter");
                                List<eMethodParam> listParam = null;
                                if (xnChilds.Count != 0)
                                {
                                    listParam = new List<eMethodParam>();
                                    foreach (XmlNode xnChild in xnChilds)
                                    {
                                        eMethodParam emp = new eMethodParam();
                                        emp.ParamName = xnChild.Attributes["name"] == null ? "" : xnChild.Attributes["name"].Value;
                                        emp.DataType = xnChild.Attributes["returnType"] == null ? "" : xnChild.Attributes["returnType"].Value;
                                        emp.OutputType = xnChild.Attributes["output"] == null ? "" : xnChild.Attributes["output"].Value;
                                        listParam.Add(emp);
                                    }
                                }
                                
                                ePro.ParamList = listParam;

                            }
                            //继承的类的列表
                            else if (xn.HasChildNodes && String.Compare(xn.Name, "Inherit", false) == 0)
                            {
                                XmlNodeList xnChildss = xn.ChildNodes;
                                List<eInherit> listInherit = null;
                                if (xnChildss.Count != 0)
                                {
                                    listInherit = new List<eInherit>();
                                    foreach (XmlNode xnChil in xnChildss)
                                    {
                                        eInherit ei = new eInherit();
                                        ei.Name = xnChil.Attributes["name"] == null ? "" : xnChil.Attributes["name"].Value;
                                        ei.Type = (enumInheritType)Enum.Parse(typeof(enumInheritType), xnChil.Name, false);
                                        listInherit.Add(ei);
                                    }
                                }
                                ePro.InheritList = listInherit;
                            }
                            //正常
                            ePro.Name = xn.Attributes["name"] == null ? "" : xn.Attributes["name"].Value;
                            ePro.PropertyType = (enumPropertyType)Enum.Parse(typeof(enumPropertyType), xn.Name, false);
                            ePro.Value = xn.Attributes["value"] == null ? "" : xn.Attributes["value"].Value;
                            ePro.ValueType = xn.Attributes["valueType"] == null ? "" : xn.Attributes["valueType"].Value;
                            ePro.ValueTypeFullName = xn.Attributes["fullType"] == null ? "" : xn.Attributes["fullType"].Value;
                            ePro.Accessibility = xn.Attributes["accessibility"] == null ? "" : xn.Attributes["accessibility"].Value;
                            ePro.ReturnType = xn.Attributes["returnType"] == null ? "" : xn.Attributes["returnType"].Value;
                            ePro.Attribute = xn.Attributes["attribute"] == null ? "" : xn.Attributes["attribute"].Value;
                            ePro.EventArgs = xn.Attributes["eventArgs"] == null ? "" : xn.Attributes["eventArgs"].Value;
                            list.Add(ePro);
                        }
                        return list;
                    }   
                }
                return list;//为空
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取基底模板所有信息
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="enumBase"></param>
        /// <returns></returns>
        public exFullInfoForBuildChild GetBaseTypeInfoFromTemplate(XmlDocument doc,enumBaseControlType enumBase)
       {
           try
           {
               exFullInfoForBuildChild exInfo = new exFullInfoForBuildChild();
               XmlNodeList xnlBase = doc.SelectSingleNode("//Document").ChildNodes;
               foreach (XmlNode xnBase in xnlBase)
               {
                   if (String.Compare(xnBase.Attributes["type"].Value, enumBase.ToString(), false) == 0)
                   {
                     exInfo.ControlFullName = xnBase.Attributes["assembly"]==null ? "" : xnBase.Attributes["assembly"].Value;
                     exInfo.ControlName = xnBase.Attributes["name"] == null ? "" : xnBase.Attributes["name"].Value;
                     exInfo.BuildName = xnBase.Attributes["name"] == null ? "" : xnBase.Attributes["name"].Value;
                     break;
                   }
               }
               return exInfo;

           }
           catch (Exception ex)
           {
               throw ex;
           }
       }


       /// <summary>
       /// 获取布局控件模板信息
       /// </summary>
       /// <param name="doc"></param>
       /// <param name="enumLay"></param>
       /// <returns></returns>
       public exFullInfoForBuildChild GetLayoutInfoFromTemplate(XmlDocument doc,enumLayoutTypeSimple enumLay)
        {
            try
            {
                exFullInfoForBuildChild exLay = new exFullInfoForBuildChild();
                XmlNode xnBase = null;
                switch (enumLay)
                {
                    case enumLayoutTypeSimple.Base:xnBase = doc.SelectSingleNode("//" + enumLayoutType.LayoutControl.ToString()); break;
                    case enumLayoutTypeSimple.Group:xnBase = doc.SelectSingleNode("//" + enumLayoutType.LayoutControlGroup.ToString());break;
                    case enumLayoutTypeSimple.Item:xnBase = doc.SelectSingleNode("//" + enumLayoutType.LayoutControlItem.ToString());break;
                    case enumLayoutTypeSimple.EmptyItem: xnBase = doc.SelectSingleNode("//" + enumLayoutType.EmptySpaceItem.ToString()); break;
                }
                exLay.BuildName=xnBase.Attributes["name"].Value;
                exLay.ControlName = xnBase.Attributes["type"].Value;
                exLay.ControlFullName = xnBase.Attributes["assembly"].Value;
                exLay.LayoutType = enumLay;
                exLay.IsBase = false;

                List<eProperty> list = new List<eProperty>();
                eProperty ep = null;
                foreach (XmlNode xn in xnBase.ChildNodes)
                {
                    ep = new eProperty();
                    ep.Name = xn.Attributes["name"].Value;
                    ep.Value = xn.Attributes["value"].Value;
                    ep.ValueType = xn.Attributes["valueType"].Value;
                    ep.ValueTypeFullName = xn.Attributes["fullType"].Value;
                    ep.Accessibility = xn.Attributes["accessibility"].Value;
                    ep.ReturnType = xn.Attributes["returnType"] == null ? "" : xn.Attributes["returnType"].Value;
                    ep.Attribute = xn.Attributes["attribute"] == null ? "" : xn.Attributes["attribute"].Value;
                    ep.PropertyType = enumPropertyType.Property;
                    list.Add(ep);
                }
                exLay.PropertyList = list;
                return exLay;
            }
            catch (Exception ex)
            {
                
                throw ex;
            }
        }
    
    }                                                                                                                           
}
